package client

import (
	"context"
	"google.golang.org/grpc"
	"kill/common/proto/goods"
	"log"
)

type Handler func(ctx context.Context, client goods.GoodsClient) (interface{}, error)

func GoodsClient(ctx context.Context, handler Handler) (interface{}, error) {
	dial, err := grpc.Dial("127.0.0.1:8081", grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	client := goods.NewGoodsClient(dial)
	res, err := handler(ctx, client)
	if err != nil {
		return nil, err
	}
	return res, nil
}
func AddKill(ctx context.Context, in *goods.AddKillReq) (*goods.AddKillResp, error) {
	client, err := GoodsClient(ctx, func(ctx context.Context, client goods.GoodsClient) (interface{}, error) {
		kill, err := client.AddKill(ctx, in)
		if err != nil {
			return nil, err
		}
		return kill, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*goods.AddKillResp), nil
}
func EsToKill(ctx context.Context, in *goods.EsToKillReq) (*goods.EsToKillResp, error) {
	client, err := GoodsClient(ctx, func(ctx context.Context, client goods.GoodsClient) (interface{}, error) {
		kill, err := client.EsToKill(ctx, in)
		if err != nil {
			return nil, err
		}
		return kill, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*goods.EsToKillResp), nil
}
func KillSearch(ctx context.Context, in *goods.KillSearchReq) (*goods.KillSearchResp, error) {
	client, err := GoodsClient(ctx, func(ctx context.Context, client goods.GoodsClient) (interface{}, error) {
		kill, err := client.KillSearch(ctx, in)
		if err != nil {
			return nil, err
		}
		return kill, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*goods.KillSearchResp), nil
}
func Register(ctx context.Context, in *goods.RegisterReq) (*goods.RegisterResp, error) {
	client, err := GoodsClient(ctx, func(ctx context.Context, client goods.GoodsClient) (interface{}, error) {
		register, err := client.Register(ctx, in)
		if err != nil {
			return nil, err
		}
		return register, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*goods.RegisterResp), nil
}
func Login(ctx context.Context, in *goods.LoginReq) (*goods.LoginResp, error) {
	client, err := GoodsClient(ctx, func(ctx context.Context, client goods.GoodsClient) (interface{}, error) {
		login, err := client.Login(ctx, in)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*goods.LoginResp), nil
}
func Realname(ctx context.Context, in *goods.RealNameReq) (*goods.RealNameResp, error) {
	client, err := GoodsClient(ctx, func(ctx context.Context, client goods.GoodsClient) (interface{}, error) {
		name, err := client.RealName(ctx, in)
		if err != nil {
			return nil, err
		}
		return name, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*goods.RealNameResp), nil

}

func CreateOrder(ctx context.Context, in *goods.CreateOrderReq) (*goods.CreateOrderResp, error) {
	client, err := GoodsClient(ctx, func(ctx context.Context, client goods.GoodsClient) (interface{}, error) {
		order, err := client.CreateOrder(ctx, in)
		if err != nil {
			log.Println(err)
			return nil, err
		}
		return order, nil
	})
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return client.(*goods.CreateOrderResp), nil
}

func AliPay(ctx context.Context, in *goods.AliPayReq) (*goods.AliPayResp, error) {
	client, err := GoodsClient(ctx, func(ctx context.Context, client goods.GoodsClient) (interface{}, error) {
		pay, err := client.AliPay(ctx, in)
		if err != nil {
			return nil, err
		}
		return pay, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*goods.AliPayResp), nil
}
